package com.ajmd.onlineexam.service;

import com.ajmd.onlineexam.entity.DepartmentPO;
import com.ajmd.onlineexam.entity.OrganizationPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.TeamPO;
import com.ajmd.onlineexam.mapper.DepartmentMapper;
import com.ajmd.onlineexam.mapper.OrganizationMapper;
import com.ajmd.onlineexam.mapper.TeacherMapper;
import com.ajmd.onlineexam.mapper.TeamMapper;
import com.ajmd.onlineexam.vo.DepartmentVO;
import com.ajmd.onlineexam.vo.OrganizationVO;
import com.ajmd.onlineexam.vo.SelectOptionVO;
import com.ajmd.onlineexam.vo.TeamVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ajmd
 * @since 2023/5/1 16:50
 */
@Service
public class OrganizationService {
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private DepartmentService departmentService;

    public int addOrUpdate(OrganizationPO organizationPO){
        if(ObjectUtils.isNotEmpty(organizationPO.getId())){
            return organizationMapper.updateById(organizationPO);
        }
        organizationPO.setId(UUID.randomUUID().toString());
        return organizationMapper.insert(organizationPO);
    }

    public Page<OrganizationPO> getPage(Integer currentPage,Integer pageSize,SearchParam searchParams){
        LambdaQueryWrapper<OrganizationPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(searchParams.getSearchValue())){
            lambdaQueryWrapper.like(OrganizationPO::getOrganizationName,searchParams.getSearchValue());
        }
        Page<OrganizationPO> page = new Page<>(currentPage,pageSize);
        return organizationMapper.selectPage(page,lambdaQueryWrapper);
    }

//    public List<OrganizationVO> getList(){
//        List<OrganizationPO> organizationPOS = organizationMapper.selectList(null);
//        List<OrganizationVO> organizationVOS = new ArrayList<>();
//        for(OrganizationPO organizationPO:organizationPOS){
//            OrganizationVO organizationVO = new OrganizationVO();
//            BeanUtils.copyProperties(organizationPO,organizationVO);
//            organizationVOS.add(organizationVO);
//        }
//        return organizationVOS;
//    }

    public List<SelectOptionVO> getList(Integer level){
        Map<String, List<SelectOptionVO>> teamVOsMap = new HashMap<>();
        if(level>2){
            List<TeamPO> teamPOS = teamMapper.selectList(null);
            if(ObjectUtils.isNotEmpty(teamPOS)){
                Map<String, List<TeamPO>> teamPOsMap = teamPOS.stream().collect(Collectors.groupingBy(TeamPO::getDepartmentId));
                //poMap转SelectOptionVOMap
                teamPOsMap.forEach((departmentId,teamPOList)->{
                    List<SelectOptionVO> teamVOList = teamPOList.stream().map(teamPO->{
                        SelectOptionVO selectOptionVO = new SelectOptionVO();
                        selectOptionVO.setValue(teamPO.getId());
                        selectOptionVO.setLabel(teamPO.getTeamName());
                        return  selectOptionVO;
                    }).collect(Collectors.toList());
                    teamVOsMap.put(departmentId,teamVOList);
                });
            }
        }


        Map<String, List<SelectOptionVO>> departmentVOsMap = new HashMap<>();
        if(level>1){
            List<DepartmentPO> departmentPOS = departmentMapper.selectList(null);
            if(ObjectUtils.isNotEmpty(departmentPOS)){
                Map<String, List<DepartmentPO>> departmentPOsMap = departmentPOS.stream().collect(Collectors.groupingBy(DepartmentPO::getOrganizationId));
                //poMap转voMap
                departmentPOsMap.forEach((organizationId,departmentPOList)->{
                    List<SelectOptionVO> departmentVOList = departmentPOList.stream().map(departmentPO->{
                        SelectOptionVO selectOptionVO = new SelectOptionVO();
                        selectOptionVO.setValue(departmentPO.getId());
                        selectOptionVO.setLabel(departmentPO.getDepartmentName());
                        if(level>2){
                            selectOptionVO.setChildren(teamVOsMap.get(departmentPO.getId()));
                        }
                        return  selectOptionVO;
                    }).collect(Collectors.toList());
                    departmentVOsMap.put(organizationId,departmentVOList);
                });
            }
        }


        List<OrganizationPO> organizationPOS = organizationMapper.selectList(null);
        List<SelectOptionVO> organizationVOS = new ArrayList<>();
        if(ObjectUtils.isEmpty(organizationPOS)){
            return organizationVOS;
        }
        for(OrganizationPO organizationPO:organizationPOS){
            SelectOptionVO organizationVO = new SelectOptionVO();
            organizationVO.setValue(organizationPO.getId());
            organizationVO.setLabel(organizationPO.getOrganizationName());
            if(level>1){
                organizationVO.setChildren(departmentVOsMap.get(organizationPO.getId()));
            }
            organizationVOS.add(organizationVO);
        }
        return organizationVOS;
    }

    public OrganizationPO getOne(String id){
        return organizationMapper.selectById(id);
    }

//    public int update(OrganizationPO organizationPO){
//        return organizationMapper.updateById(organizationPO);
//    }
//

    @Transactional
    public int delete(String id){
        departmentService.deleteByOrganizationId(id);
        return organizationMapper.deleteById(id);
    }
}
